package com.kingsalert.kingsalert.bean.config;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.kingsalert.kingsalert.R;
import com.kingsalert.kingsalert.util.UserInfoUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class AppShowCustomize {

    private static class Inner {
        private static AppShowCustomize instance = new AppShowCustomize();
    }

    private AppShowCustomize() {}

    public static AppShowCustomize getInstance() {
        return Inner.instance;
    }

    public static void parse(String jsonStr) {

        Inner.instance = new Gson().fromJson(jsonStr,AppShowCustomize.class);

        Inner.instance.recommendAppList = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);

            JSONArray recommendAppListJsarray= jsonObject.optJSONArray("recommendAppList");
            if (recommendAppListJsarray!=null){
                for (int i=0;i<recommendAppListJsarray.length();i++){
                    JSONObject recommendInfoJsobject = recommendAppListJsarray.optJSONObject(i);
                    if (recommendInfoJsobject!=null){
                        Inner.instance.recommendAppList.add(RecommendAppInfo.getInstance(recommendInfoJsobject.toString()));
                    }
                }
            }

            JSONObject loginInfoJson = jsonObject.optJSONObject("loginInfo");
            if (loginInfoJson != null) Inner.instance.loginInfo = AppShowLoginInfo.getInstance(loginInfoJson.toString());


            //-----kingsalert------
            ArrayList<RecommendAppInfo> recommendAppList = Inner.instance.recommendAppList;
            AppShowLoginInfo loginInfo1 = Inner.instance.loginInfo;

            Log.w("---recommendAppList---", "Size:"+recommendAppList.size());
            Log.w("---loginInfo---", loginInfo1 == null ? "NULL":loginInfo1.getAmountGranularity());

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    //参数
    private String mobile;//客服电话
    private String email;//客服邮箱
    private String customerServiceHint;//客服提示文案
    private String agreementUrl;//协议链接
    private JsonArray accList;//测试账号
    private JsonArray walletChannels;//法语区绑卡渠道列表
    private String rejectPeriod;//借款失败后间隔时间
    private String showSysReview;//评分弹窗控制
    private AppShowLoginInfo loginInfo;//登录页信息
    private AppShowPermissions permissions;//权限配置
    private String recommendFlag;//app推荐开关（YES/NO）
    private ArrayList<RecommendAppInfo> recommendAppList;//app推荐列表


    public String[] getMobiles(){
        if (TextUtils.isEmpty(mobile)) return new String[0];
        return mobile.split(",");
    }

    public String[] getEmails(){
        if (TextUtils.isEmpty(email)) return new String[0];
        return email.split(",");
    }

    public String getCustomerServiceHint() {
        if (TextUtils.isEmpty(customerServiceHint)) return "";
        return customerServiceHint;
    }

    public String getAgreementUrl() {
        if (TextUtils.isEmpty(agreementUrl)) return "";
        return agreementUrl;
    }

    public String getTestToken(String mobile){
        if (accList == null || accList.size()<1) return "";
        for (int i=0;i<accList.size();i++){
            try {
                JSONObject accJson = new JSONObject(accList.get(i).toString());
                if (accJson.optString("accM").equalsIgnoreCase(mobile)){
                    return accJson.optString("accTk");
                }
            }catch (JSONException e){
                e.printStackTrace();
            }
        }
        return "";
    }

    public boolean isTestAccount(String mobile){
        return !TextUtils.isEmpty(getTestToken(mobile));
    }

    public boolean isTestAccount(){
        return !TextUtils.isEmpty(getTestToken(UserInfoUtil.getInstance().getMobile()));
    }

    public ArrayList<AccountChannel_Fr> getAccountChannels(){
        ArrayList<AccountChannel_Fr> channels = new ArrayList<>();
        if (walletChannels==null || walletChannels.isEmpty()) return channels;
        Gson gson = new Gson();
        for (int i=0;i<walletChannels.size();i++){
            AccountChannel_Fr channel = gson.fromJson(walletChannels.get(i).toString(),AccountChannel_Fr.class);
            channels.add(channel);
        }
        return channels;
    }

    public String getRejectPeriod(Context context) {
        if (TextUtils.isEmpty(rejectPeriod)) return "7 "+context.getString(R.string.days);
        return rejectPeriod+" "+context.getString(R.string.days);
    }

    public String[] getHomePermission(){
        if (permissions == null) return new ArrayList<String>().toArray(new String[0]);
        ArrayList<String> home = permissions.getHome();
        return home.toArray(new String[0]);
    }

    public boolean getRecommendFlag(){
        if (TextUtils.isEmpty(recommendFlag)) return false;
        return recommendFlag.equalsIgnoreCase("YES");
    }

    public ArrayList<RecommendAppInfo> getRecommendAppList() {
        if (recommendAppList == null) new ArrayList<>();
        return recommendAppList;
    }

    public boolean showSysReview(){
        if (TextUtils.isEmpty(showSysReview)) return false;
        return showSysReview.equalsIgnoreCase("YES");
    }


    public String getLoginAmountGranularity() {
        if (loginInfo == null) return "";
        return loginInfo.getAmountGranularity();
    }

    public String getLoginMinAmount() {
        if (loginInfo == null) return "";
        return loginInfo.getMinAmount();
    }

    public String getLoginMaxAmount() {
        if (loginInfo == null) return "";
        return loginInfo.getMaxAmount();
    }

    public String getLoginInterestRate() {
        if (loginInfo == null) return "";
        return loginInfo.getInterestRate();
    }

    public String getLoginInterestRateCycle() {
        if (loginInfo == null) return "";
        return loginInfo.getInterestRateCycle();
    }


}
